package com.example.sjc715.main.ui.dashboard

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.commonlibrary.mvi.BaseFragment
import com.example.commonlibrary.util.CommonAdapter
import com.example.net_library.entity.SimpleVideoEntity
import com.example.sjc715.BR
import com.example.sjc715.R
import com.example.sjc715.databinding.FragmentCinemaBinding
import com.example.sjc715.main.ui.home.HomeIntent
import com.example.sjc715.main.ui.home.HomeState

@Route(path = "/home/cinema")
class CinemaFragment : BaseFragment<FragmentCinemaBinding,CinemaViewModel>() {
    @Autowired
    @JvmField
    var channel:String = ""
    val adapter by lazy {
        CommonAdapter<SimpleVideoEntity>({R.layout.cinema to BR.video})
    }
    override fun initView() {
        ARouter.getInstance().inject(this)
        viewModel.intent.trySend(CinemaIntent.loadVideo(channel, 10, 30))
        binding.rec.adapter = adapter
    }

    override fun initData() {
        viewModel.state.observe(this){
            when(it){
                is CinemaState.VideoSuccess ->{
                    adapter+=it.video.data
                }
            }
        }
    }
    override fun onResume() {
        super.onResume()
        adapter.clear()
        viewModel.intent.trySend(CinemaIntent.loadVideo(channel, 10, 30))
    }
}